package com.jobsys.controller;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jobsys.model.MUserinfo;

public class UserController extends Controller {

	public void index() {
		if (getCookieObject("userID") != null && getCookieObject("userpwd") != null) {
			render("/jobpage.jsp");
		} else {
			render("/login.jsp");
		}
	}

	/**
	 * 登录
	 */
	public void login() {
		String userid = this.getPara("userid");
		String pwd = this.getPara("pwd");
		MUserinfo userinfo = MUserinfo.dao.findByUserID(userid);

		String re = "";
		boolean s = false;
		if (null == userinfo) {
			re = "用户不存在";
		} else if (pwd.equals(userinfo.getStr("pwd"))) {
			re = "登录成功！";
			s = true;

			this.setCookie("userID", userinfo.getStr("userID"), 1800);
			this.setCookie("userpwd", userinfo.getStr("pwd"), 1800);
			this.setCookie("right", userinfo.getInt("rightx").toString(), 1800);

			// session 暂时不用
			this.setSessionAttr("userID", userinfo.getStr("userID"));
			this.setSessionAttr("userpwd", userinfo.getStr("pwd"));
			this.setSessionAttr("right", userinfo.getInt("rightx").toString());
		} else {
			re = "登录失败！";
		}

		JSONObject json = new JSONObject();
		json.put("msg", re);
		json.put("success", s);
		if (s) {
			json.put("right", userinfo.getInt("rightx").toString());
		}

		this.renderJson(json);
	}

	/**
	 * 注册
	 */
	public void register() {
		String userid = this.getPara("userid");
		String pwd = this.getPara("pwd");

		String re = "";
		boolean s = false;
		if (userid.trim() == "" || pwd.trim() == "") {
			re = "用户名或密码不合法";
		} else {
			try {
				MUserinfo userinfo = MUserinfo.dao.findByUserID(userid);

				if (null != userinfo) {
					re = "用户已存在！";
				} else {
					MUserinfo.dao = new MUserinfo();
					MUserinfo.dao.set("userID", userid);
					MUserinfo.dao.set("pwd", pwd);
					MUserinfo.dao.set("nickname", userid);
					MUserinfo.dao.set("addtime", new Date());
					MUserinfo.dao.save();
					re = "注册成功！";
					s = true;
				}
			} catch (Exception ex) {
				re = ex.getMessage();
			}
		}

		JSONObject json = new JSONObject();
		json.put("msg", re);
		json.put("success", s);

		this.renderJson(json);
	}

	// 修改用户信息
	public void updateUserinfo() {
		try {
			String id = this.getPara("id");
			String userid = this.getPara("userid");
			String pwd = this.getPara("pwd");
			String nickname = this.getPara("nickname");
			String right = this.getPara("rightx");
			String isdelete = this.getPara("isdelete");

			String re = "";

			MUserinfo userinfo = MUserinfo.dao.findByUserID(userid);

			if (null == userinfo) {
				re = "用户不存在！";
			} else {
				if (null == right || "" == right) {
					right = userinfo.getStr("right");
				}

				MUserinfo.dao.updateState(Integer.parseInt(id), userid, nickname, pwd, Integer.parseInt(right),
						Integer.parseInt(isdelete));
				re = "更新成功！";
			}

			JSONObject json = new JSONObject();
			json.put("msg", re);

			this.renderJson(json);
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}

	/**
	 * 退出
	 */
	public void exit() {
		String re = "退出成功！" + "";
		boolean s = true;
		this.removeCookie("userID");
		this.removeCookie("userpwd");
		this.removeCookie("right");

		JSONObject json = new JSONObject();
		json.put("msg", re);
		json.put("success", s);

		this.renderJson(json);
	}

	/**
	 * 查询所有用户
	 */
	@SuppressWarnings("unused")
	public void queryAllUser() {
		String page = this.getPara("pageNumber");
		String pagesize = this.getPara("pageSize");
		List<MUserinfo> userinfo = MUserinfo.dao.findAll(Integer.parseInt(page), Integer.parseInt(pagesize));
		int count = MUserinfo.dao.Count();

		JSONObject json = new JSONObject();
		json.put("rows", userinfo);
		json.put("total", count);

		renderJson(json);
	}

	public void deleteUser() {
		String id = this.getPara("userid");
		String userid = this.getPara("userid");
		MUserinfo.dao.deleteUser(id, userid);

		renderJson("msg", "删除成功！");
	}
}
